package com.cqgc.controller;

import com.cqgc.domain.*;
import com.cqgc.service.ColonyService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@RestController
@RequestMapping("colony")
public class ColonyController {
    @Autowired
    private ColonyService service;

    ResultColony result=new ResultColony();

    @PostMapping("del")
    public ResultColony delColony(@RequestBody Colony colony){

        int i = service.delColony(colony);
        if (i>0){
            result.setCode(1);
            result.setMsg("DelSuccess!");
        }else{
            result.setCode(0);
            result.setMsg("DelLost!");
        }

        return result;
    }

    @PostMapping("stop")
    public ResultColony stopUse(@RequestBody Colony colony){

        if (service.stopUse(colony)>0){
            result.setCode(1);
            result.setMsg("StopSuccess!");
        }else{
            result.setCode(0);
            result.setMsg("StopLost!");
        }

        return result;
    }

    @PostMapping("edit")
    public ResultColony updateColony(@RequestBody Colony colony){

        if (service.update(colony)>0){
            result.setCode(1);
            result.setMsg("StopSuccess!");
        }else{
            result.setCode(0);
            result.setMsg("StopLost!");
        }

        return result;
    }

    @PostMapping("add")
    public ResultColony addColony(@RequestBody Colony colony){
        if (colony!=null){
            colony.setCcount(colony.getCcount());
            colony.setCname(colony.getCname());
            colony.setFid(colony.getFid());
            colony.setCtag(colony.getCtag());
            colony.setCwarm(colony.getCwarm());
        }

        if (service.addColony(colony)>0){
            result.setCode(1);
            result.setMsg("addSuccess!");
        }else{
            result.setCode(0);
            result.setMsg("addLost!");
        }

        return result;
    }

    @PostMapping("findByFather")
    public ResultColony findByFather(@RequestBody Colony colony){
        List<Colony> count= service.findByFather(colony.getFid());
        if (count!=null){
            result.setCode(1);
            result.setMsg("Success!");
            result.setData(count);
            result.setTotal(count.size());
        }else {
            result.setCode(0);
            result.setMsg("查询失败");
        }

        return result;
    }
    @PostMapping("colonylist")
    public ResultColony findAll(@RequestBody PageHelper pageHelper){
        if (pageHelper!=null){
            int page = (pageHelper.getCurrentPage()-1)*pageHelper.getPageSize();
            pageHelper.setCurrentPage(page);
        }
        List<Colony> colonyList = service.findPage(pageHelper);
        List<Colony> count= service.findAll();
        if (colonyList!=null){
            result.setCode(1);
            result.setMsg("Success!");
            result.setData(colonyList);
            result.setTotal(count.size());
        }else {
            result.setCode(0);
            result.setMsg("查询失败");
        }

        return result;
    }

    //    查找状态正常的牧场
    @PostMapping("findGood")
    public ResultColony searchGood(Colony colony){
        List<Colony> count=service.findGood();
        if (count!=null){
            result.setCode(1);
            result.setMsg("Success!");
            result.setData(count);
            result.setTotal(count.size());
        }else {
            result.setCode(0);
            result.setMsg("查询失败");
        }

        return result;
    }

    //    查找bu状态正常的牧场
    @PostMapping("findStop")
    public ResultColony searchStop(Colony colony){
        List<Colony> count=service.findStop();
        if (count!=null){
            result.setCode(1);
            result.setMsg("Success!");
            result.setData(count);
            result.setTotal(count.size());
        }else {
            result.setCode(0);
            result.setMsg("查询失败");
        }

        return result;
    }

    //    模糊查询，搜索牧场
    @PostMapping("search")
    public ResultColony searchC(@RequestBody Colony colony){
//        List<Harvest> all = service.findAll(pageHelper);
        System.out.println("======================");
        System.out.println(colony.getCname());
        System.out.println("======================");
        List<Colony> count=service.searchC(colony.getCname());
        if (count!=null){
            result.setCode(1);
            result.setMsg("Success!");
            result.setData(count);
            result.setTotal(count.size());
        }else {
            result.setCode(0);
            result.setMsg("查询失败");
        }

        return result;
    }


}
